#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define MAXLINE 80
#define SERV_PORT 8000

int main(int argc, char **argv)
{
    struct sockaddr_in serv_addr;
    char buf[MAXLINE];
    int sock_fd, n;
    char *str;

    if (argc != 2) {
        fputs("usage: ./client ...message...\n", stderr);
        exit(1);
    }
    str = argv[1];

    sock_fd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
    serv_addr.sin_port = htons(SERV_PORT);

    connect(sock_fd, (struct sockaddr *)&serv_addr, \
            sizeof(serv_addr));

    write(sock_fd, str, strlen(str));

    n = read(sock_fd, buf, MAXLINE);
    write(STDOUT_FILENO, "Response from server:", MAXLINE);
    write(STDOUT_FILENO, buf, n);
    putchar('\n');

    close(sock_fd);
    return 0;
}

